package com.nf.health.app.fragment;

import android.annotation.SuppressLint;
import android.hardware.SensorEventListener;

@SuppressLint("NewApi")
public class HealthyBalanceFragment2 extends IBaseFragment {
/*
	private static final String TAG = HealthyBalanceFragment2.class.getSimpleName();
	private ImageView shake_bg;
	private TextView tv_shake_title;
	private SensorManager mSensorManager = null;
	private Vibrator mVibrator = null;
	private String mDeviceAddress = "";
	private int count = 0;
	// 蓝牙
	private byte[] b;
	private TextView tv_height, tv_weight, tv_fat, tv_bone, tv_jiroulv,
	tv_subCutaneousFat, tv_waterContent, tv_calorie, tv_age;
	private ScrollView scr_view;
	private RelativeLayout rl_shake;
	private boolean flg = true;

	private Button btn_data_commit;
	private int age;
	private int height;
	private float weight;
	private int fat;
	private int bone = 0;
	private int jiRou;
	private int piXiaZhiFang;
	private int shuiFen;
	private int bMR;
	private int user;
	private int sex;
	Intent serviceIntent;
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		addViewFillInRoot(R.layout.fragment_balance_auto_input);

		initView();

		try {
			initBluetooth();
		} catch (Exception e) {
		}
	}

	private void initBluetooth() {
		// 检查当前手机是否支持ble 蓝牙,如果不支持退出程序
		if (!getActivity().getPackageManager().hasSystemFeature( PackageManager.FEATURE_BLUETOOTH_LE)) {
			showToast("不支持蓝牙4.0通讯");
			tv_shake_title.setText("该设备不支持蓝牙4.0通讯");
			return;
		}

		// 初始化 Bluetooth adapter, 通过蓝牙管理器得到一个参考蓝牙适配器(API必须在以上android4.3或以上和版本)
		// final BluetoothManager bluetoothManager = (BluetoothManager)
		// getActivity().getSystemService(Context.BLUETOOTH_SERVICE);
		BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

		// 检查设备上是否支持蓝牙
		if (mBluetoothAdapter == null) {
			showToast("设备不支持蓝牙");
			return;
		}
		if (!mBluetoothAdapter.isEnabled()) {
			mBluetoothAdapter.enable();
		}

		mSensorManager = (SensorManager) getActivity().getSystemService(Service.SENSOR_SERVICE);
		mVibrator = (Vibrator) getActivity().getSystemService(Service.VIBRATOR_SERVICE);

		init();
	}

	private void init() {
		// 开启后台service
		// 开启扫描
		serviceIntent = new Intent(getActivity(), TimeService.class);
		getActivity().startService(serviceIntent);

		// 注册BoradcasrReceiver
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(BluetoothTools.ACTION_NOT_FOUND_SERVER);
		intentFilter.addAction(BluetoothTools.ACTION_FOUND_DEVICE);
		intentFilter.addAction(BluetoothTools.ACTION_DATA_TO_GAME);
		intentFilter.addAction(BluetoothTools.ACTION_CONNECT_SUCCESS);
		intentFilter.addAction(BluetoothTools.ACTION_CONNECT_ERROR);
		intentFilter.addAction(BluetoothTools.ACTION_READ_DATA);
		getActivity().registerReceiver(broadcastReceiver, intentFilter);
	}


	private void initView() {
		tv_shake_title = (TextView) rootView.findViewById(R.id.tv_shake_title);
		shake_bg = (ImageView) rootView.findViewById(R.id.shake_bg);
		mSensorManager = (SensorManager) getActivity().getSystemService(Service.SENSOR_SERVICE);
		mVibrator = (Vibrator) getActivity().getSystemService(Service.VIBRATOR_SERVICE);

		rl_shake = (RelativeLayout) rootView.findViewById(R.id.rl_shake);
		scr_view = (ScrollView) rootView.findViewById(R.id.scr_view);
		tv_height = (TextView) rootView.findViewById(R.id.height);
		tv_weight = (TextView) rootView.findViewById(R.id.weight);
		tv_fat = (TextView) rootView.findViewById(R.id.fat);
		tv_bone = (TextView) rootView.findViewById(R.id.bone);
		tv_jiroulv = (TextView) rootView.findViewById(R.id.jiroulv);
		tv_subCutaneousFat = (TextView) rootView.findViewById(R.id.subCutaneousFat);
		tv_waterContent = (TextView) rootView.findViewById(R.id.waterContent);
		tv_calorie = (TextView) rootView.findViewById(R.id.calorie);
		tv_age = (TextView) rootView.findViewById(R.id.age);

		btn_data_commit = ViewUtil.getView(getActivity(), R.id.btn_data_commit);
		btn_data_commit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				provider.requestAddDeviceBalance(String.valueOf(weight),
						String.valueOf(height), String.valueOf(fat),
						String.valueOf(bone), String.valueOf(jiRou),
						String.valueOf(piXiaZhiFang), String.valueOf(shuiFen),
						String.valueOf(bMR), String.valueOf(age),
						"ADDDEVICEBALANCE");
			}
		});
	}

	@Override
	public void handleActionSuccess(String actionName, Object object) {
		showToast((String) object);
		getActivity().finish();
	}



	@Override
	public void onResume() {
		super.onResume();
		if (mSensorManager != null) {
			mSensorManager.registerListener(this, mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
		}
	}

	@Override
	public void onPause() {
		super.onPause();
		if (mSensorManager != null) {
			mSensorManager.unregisterListener(this);
		}
	}

	@Override
	public void onDestroy() {
//		if (mBluetoothAdapter != null) {
//			mBluetoothAdapter.disable();
//		}

		Intent startService = new Intent(BluetoothTools.ACTION_STOP_SERVICE);
		getActivity().sendBroadcast(startService);
		PollingUtils.stopPollingService(getActivity(), ScaneBluetoothService.class, ScaneBluetoothService.ACTION);
		if(null!=serviceIntent)
			getActivity().stopService(serviceIntent);

		if (null != BluetoolUtil.mBluetoothAdapter) {
			BluetoolUtil.mBluetoothAdapter.disable();
		}
		getActivity().unregisterReceiver(broadcastReceiver);
		super.onDestroy();
	}


	*/
/**
	 * 写入的指令数据 测试指令FE 03 01 00 AA 19 01 B0 byte[7] = byte[1]^....byte[6] 异或
	 * 
	 * @return
	 *//*

	@SuppressLint("SimpleDateFormat")
	public byte[] getByteData() {
		String sex = PreferenceHelper.getString(PreferenceHelper.USERSEX, "1");
		String height = PreferenceHelper.getString(PreferenceHelper.USERHEIGHT,
				"170");
		b = new byte[8];
		b[0] = (byte) 0xfe;// 包头
		b[1] = (byte) 0x03;
		if (sex.equals("1")) {// 性别
			b[2] = (byte) 0x01;
		} else {
			b[2] = (byte) 0x00;
		}
		b[3] = (byte) 0x00;// 0普通 1业余 2专业
		b[4] = Integer.valueOf(height).byteValue();// 身高
		try {
			SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
			Date date = s.parse(PreferenceHelper.getString(
					PreferenceHelper.BIRTHDAY, "1991-01-01"));
			int age = Util.getAgeByBirthday(date);
			if (age < 10) {
				age = 10;
			}
			b[5] = Integer.valueOf(age).byteValue();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// b[4] = (byte) 0xaa;
		// b[5] = (byte) 0x19;
		b[6] = (byte) 0x01;// 单位 01千克 02lb(英石) 03ST:LB(英磅)
		b[7] = (byte) (b[1] ^ b[2] ^ b[3] ^ b[4] ^ b[5] ^ b[6]);
		return b;
	}

	// cf 11 9e a8 02a7 00000000000000000000
	// CF 03 98 A1 02A4 00000000000000000000
	public void get10Code(String msg) {
		String[] b = new String[(msg.length()) / 2];
		for (int i = 0; i < (msg.length()) / 2; i++) {
			b[i] = msg.trim().substring(i * 2, 2 * (i + 1));
		}
		user = Integer.parseInt(Util.toD(b[0], 16)) & 0x0f;
		sex = (Integer.parseInt(Util.toD(b[1], 16)) >> 7);
		age = Integer.parseInt(b[2], 16) & 127;
		height = Integer.parseInt(b[3], 16);
		int weightTemp = Integer.parseInt(b[4]+b[5], 16);
		fat = Integer.parseInt(b[6]+b[7],16);
		bone = Integer.parseInt(b[8], 16);
		jiRou = Integer.parseInt(b[9]+b[10],16);
		piXiaZhiFang = Integer.parseInt(b[11], 16);
		shuiFen = Integer.parseInt(b[12]+b[13], 16);
		bMR = Integer.parseInt(b[14]+b[15], 16);
		tv_height.setText(height + " CM");
		weight = weightTemp / 10.0f;
		tv_weight.setText(weight + " KG");
		tv_fat.setText(fat + "%");
		tv_bone.setText(bone + " KG");
		tv_jiroulv.setText(jiRou + "%");
		tv_subCutaneousFat.setText(piXiaZhiFang + " mm");
		tv_waterContent.setText(shuiFen + " ppm");
		tv_calorie.setText(bMR + " KG");
		if(age==10){
			tv_age.setText("小于"+age + " 岁");
		}else{
			tv_age.setText(age + " 岁");
		}

		rl_shake.setVisibility(View.GONE);
		scr_view.setVisibility(View.VISIBLE);
	}

	@Override
	public void onSensorChanged(SensorEvent event) {
		int sensorType = event.sensor.getType();
		float[] values = event.values;
		if (sensorType == Sensor.TYPE_ACCELEROMETER && flg) {
			if (Math.abs(values[0]) > 14 || Math.abs(values[1]) > 14
					|| Math.abs(values[2]) > 14) {
				long[] pattern = { 500, 1000 };
				mVibrator.vibrate(pattern, -1);

				ObjectAnimator animator = ObjectAnimator.ofFloat(shake_bg, "translationX", 0, 100);
				animator.setRepeatCount(3);
				animator.setRepeatMode(ObjectAnimator.REVERSE);
				animator.start();

				tv_shake_title.setText("设备扫描中...");
				mDeviceAddress = "";

				PollingUtils.startPollingService(getActivity(), 10, ScaneBluetoothService.class, ScaneBluetoothService.ACTION);
			}
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}


	private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			String action = intent.getAction();

			if (BluetoothTools.ACTION_START_DISCOVERY.equals(action)) {
//				tile.setText("Start Scaning..");
			}else if (BluetoothTools.ACTION_NOT_FOUND_SERVER.equals(action)) {
				tv_shake_title.setText("没有连接到设备");
			} else if (BluetoothTools.ACTION_FOUND_DEVICE.equals(action)) {
				// »ñÈ¡µ½Éè±¸¶ÔÏó
				if(null!= BluetoolUtil.lastDevice){
					//String address = BluetoolUtil.lastDevice.getAddress();
					String name = BluetoolUtil.lastDevice.getName();
					tv_shake_title.setText("发现设备："+name);
				}

			} else if (BluetoothTools.ACTION_CONNECT_SUCCESS.equals(action)) {
				if(null!=BluetoolUtil.lastDevice){
					//String address = BluetoolUtil.lastDevice.getAddress();
					String name = BluetoolUtil.lastDevice.getName();
					tv_shake_title.setText("连接设备：" + name);
				}

			} else if (BluetoothTools.ACTION_DATA_TO_GAME.equals(action)) {
				if(null!=BluetoolUtil.lastDevice){
					//String address = BluetoolUtil.lastDevice.getAddress();
					String name = BluetoolUtil.lastDevice.getName();
				}

			}else if(BluetoothTools.ACTION_CONNECT_ERROR.equals(action)){
				if(null!=BluetoolUtil.lastDevice){
					//String address = BluetoolUtil.lastDevice.getAddress();
					String name = BluetoolUtil.lastDevice.getName();
					tv_shake_title.setText("连接失败");
				}

			}else if(BluetoothTools.ACTION_READ_DATA.equals(action)){
				String msg = intent.getStringExtra("readMessage");
				get10Code(msg);
				PollingUtils.stopPollingService(getActivity(),ScaneBluetoothService.class, ScaneBluetoothService.ACTION);
			}
		}
	};
*/
}
